package com.shop.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 支付宝配置类
 * 用于配置支付宝客户端所需的参数
 */
@Configuration
public class AlipayConfig {
    /**
     * 支付宝网关
     * 沙箱环境使用：https://openapi.alipaydev.com/gateway.do
     * 正式环境使用：https://openapi.alipay.com/gateway.do
     */
    private static final String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    /**
     * 应用ID
     * 在支付宝开放平台创建应用后获取
     */
    private static final String APP_ID = "2021000148669770";

    /**
     * 商户私钥
     * 使用支付宝开放平台提供的工具生成
     * 注意：需要妥善保管，不要泄露
     */
    private static final String PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCFw22BYZj8PWQBrxow2VPBQvCXshX5LUaXoGcCrFAGqzqIixPRtbYronUBsM8HDAU/nnFs77TdApMT1Izd9wZAsFU+vowdYUCDcWq0un28RgQzA7z/mFYedr2hSetjBfto8QfOt16tmToBOlQ9pS/UP7BB0ioWKXajmt81lfpJ0ayqr0kBglkzLBsLrWr37aO0p/CzSDMl7xAjxNfhyb6QpsPM5LbRqO8wUXd0QG3yQCyj5tuu1C9ZvKUkqM7RIjgcQyMEYGhFkWdhQ0+vxXzO4zoB4D4BsOoXGKdsN8pv22fV36bKQV8WdbLlacBj6K1RrXh1k75SpaS5XT6CX64lAgMBAAECggEAB/CLpWxXmlFCiF8OXOvpbsU8ZqSpbTrkv8dDlgMIVP0A4m3u5Tm18G0aKNmH0FFEVnxfGyo3QA8RD9JD8kDhXCGbTbgdtPi6zZKD1Opk4DH/uc90/m7aXcCKjPJISH5oZ9g2sGxt8DvyF3SQ33/YkO6e1alV2i7n3dopeZLHYm47VzIoCNjbRwvhCnU64WgZpgyKzKOF1HbZ5N1L7NCwlr1OYjPgyPs2tyJ1TUEnu4NOaF9Z/rGYJjIFArOuPFuefeMsEUc4lfCNAAPtquewGmq/stIidb7xsu3c2mx95pRbV+dERbHhW9TJzheq15ZAit9lJPZ1vX4uRmxjLPCtQQKBgQDcCI6tM2Y5aPay8IPpay41UfLu8rvdoT4loIhtu/ZnekY3TMBqXm8aUUDSEi13aFCxM26WoanIdsxk/OAP8/t4TMT/shOwdqgsgQ+mNO1QHsCRkezWSocBmjtMkvsxxnN/qShO2mHptF+WcFcHDgBSu2W7jo83Akv/NdICh3npjQKBgQCboNgjC0EZ35YkKxH9XKRhNrvLlCvTeqSm4QCbf2sBTxyw3CBqqoGVihfbPAGGOS8w9rzRv+6XVhgu/1Tb0VCuEwkhlK7IlEP8Ubp2F+ixONT+rSo0yV3Ng5sqIWhFzXPjrrdU+mR04CsQBvE7KVkkrj01DCkSkgiVjFGcgF6U+QKBgEmMCrTWI2s5LIzvXAhfrRhhhgRc2nLgfLdzC2cyltBLzMCbRQ3I9vYjBrmxHcV19tLkUCjFbMz2+Kcg53wtn3a8onSdf3tSjjAiC6sQseo//TorPrD6RFpCxWN5FcGzhdO8JR8GXAgUJIdSkaRoB6+fuAeTRUer2t85jDMQOa3ZAoGALkcF/mW6VhdGbjrgfpatyrqb3U2SOrCdhYIizBp3l0wPHAhZG17Yab9GmMqfKWqXvdehDj4klJvlVRRYB4WWmCzQ6ktsLfjovn8PE07IwupDa/fSNSAr5JDd8wk8GIWfkHTDz2ElvhtqsrldwlnpSB8H1F5pWDxUkSPIoOELPwECgYEAvWDyyGKB14NzmSmnJoY4Tlj1zeP9ZaUm2/EbzdrsgrYj1LkcsbUChyN4UbztDWC7Uxm3URsuort/XyVYevQEEUrlW5dDTYrNcLJ5duLWtnt6/v1t3FnNz5NMgAwjFZ6vJKhNkj9tD+jPmH/b8KsQhPtd3Td6Ph02HQSCei+8dzI=";

    /**
     * 支付宝公钥
     * 在支付宝开放平台获取
     * 用于验证支付宝的异步通知
     */
//    public static final String CHARSET = "UTF-8";
    private static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp2dMghOghpY9NCni67CTeKUJpBADw/Lohl7YBBnVbwsSkziiBFENYaUcFG/GLF0rudqM0Bzt7M7+5Putiqo5cwXlIwqOJtziLgXya8HqnseDZtAWpmixfy5yqnlbijkteX/3/IZ2IFWg2OJC1V/WBz3URN4l3j4JSc18k7QiSB+Y5ySLb5lhzkDNOl/80IXiZ+bg3Ok5JIC0w8iEukfnPgTiopHnp9H5HAHuHPylOsm+FLGBZADemGKyZPBoJqGfbdVmojHNhsNmZwul+l6txLvklhxqlom8xoI7t1Cz2DwZwBfdISxSJjq8CACMGx1Hn8d8jaTB4BnYDLe3cJGk9QIDAQAB";
    /**
     * 异步通知地址
     * 支付宝服务器主动通知商户服务器里指定的页面http/https路径
     * 建议使用https协议
     */
    public static final String NOTIFY_URL = "http://localhost:8080/shop/order/alipay/notify";

    /**
     * 同步返回地址
     * 支付完成后，用户从支付宝页面跳转回商户页面的地址
     */
    public static final String RETURN_URL = "http://localhost:8080/shop/order/alipay/return";
    /**
     * 创建支付宝客户端
     * @return AlipayClient实例
     */
    @Bean
    public AlipayClient alipayClient() {
        return new DefaultAlipayClient(
                URL,                // 支付宝网关
                APP_ID,            // 应用ID
                PRIVATE_KEY,       // 商户私钥
                "json",           // 请求格式
                "UTF-8",          // 编码格式
                ALIPAY_PUBLIC_KEY, // 支付宝公钥
                "RSA2"            // 签名方式
        );
    }
}